import os
import requests

def receive_file():
    # 指定路径
    path = "D:/火电巡检系统/103"

    # 遍历文件夹
    for folder in os.listdir(path):
        try:
            catalog = int(folder)
            inspectionGroupId = catalog // 100
            inspectionDetailId = catalog % 100
            print("inspectionGroupId:", inspectionGroupId)
            print("inspectionDetailId:", inspectionDetailId)

        except ValueError:
            print(f"无法将文件夹名‘{folder}’转换为整数")
            continue

        # 构建完整的 URL
        url = f"http://localhost:8090/api/inspection/submit/{inspectionGroupId}/{inspectionDetailId}"

        # 创建多部分表单数据
        files = []
        folders = os.path.join(path, folder)

        # 添加图像文件
        for filename in os.listdir(folders):
            if filename.endswith('.jpg'):
                image_path = os.path.join(folders, filename)
                with open(image_path, 'rb') as f:
                    files.append(('imageList', (filename, f.read(), 'image/jpeg')))  # 使用 'imageList'

        # 添加音频和 JSON 文件
        for filename in os.listdir(folders):
            if filename.endswith('.mp3'):
                radio_path = os.path.join(folders, filename)
                with open(radio_path, 'rb') as f:
                    files.append(('cheatMsg', (filename, f.read(), 'audio/mpeg')))
            elif filename.endswith('.json'):
                data_path = os.path.join(folders, filename)
                with open(data_path, 'rb') as f:
                    files.append(('dataListJson', (filename, f.read(), 'application/json')))

        # 发送请求
        response = requests.post(url, files=files)

        # 处理响应
        if response.status_code == 200:
            print("请求成功:", response.json())  # 或者根据需要处理响应内容
        else:
            print("请求失败，状态码:", response.status_code, response.text)

# 调用函数
receive_file()
